const Goods = require('../model/goods.model')

class GoodsService{
    async createGoods(goods){
        const res = await Goods.create(goods)
        return res.dataValues
    }

    async updateGoods(id, goods){
        const res = await Goods.update(goods, {where: {id}})

        return !!res[0] //修改成功返回[1]，修改失败返回[0],所以在这里做一个判断
    }

    async removeGoods(id){
        const res =  await Goods.destroy({where : {id}})
        return !!res //注意：这里返回的是0和1，而不是数组
    }

    async restoreGoods(id){
        const res = await Goods.restore({where : {id}}) //将deteledAt设置为null
        return !!res   
    }

    async findGoods(pageNum, pageSize){
        // // 1、获取总数
        // const count = await Goods.count()
        // // 2、获取分页具体数据
        // const offset = (pageNum - 1) * pageSize
        // const rows = await Goods.findAll({offset: offset, limit: (pageSize * 1)})


        // 合并上面两步
        const offset = (pageNum - 1) * pageSize
        const {count, rows} = await Goods.findAndCountAll({offset: offset, limit:(pageSize * 1)}) //findAndCountAll合并了上面两个方法

        return {
            pageNum,
            pageSize,
            total: count,
            list: rows,
        }



        return res
    }
}

module.exports = new GoodsService()